package com.dkd.testUser.service.impl;

import com.dkd.common.core.redis.RedisCache;
import com.dkd.testUser.domain.TestShop;
import com.dkd.testUser.domain.TestUser;
import com.dkd.testUser.mapper.TestUserMapper;
import com.dkd.testUser.service.ITestUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 测试用户表 服务实现类
 * </p>
 *
 * @author zwy
 * @since 2025-04-28
 */
@Service
public class TestUserServiceImpl implements ITestUserService {
    @Autowired
    private  TestUserMapper testUserMapper;
    @Autowired
    private RedisCache redisCache;


    @Override
    public List<TestUser> selectList() {
        return testUserMapper.selectList();
    }

    @Override
    public TestUser selectById(Integer id) {
        if (redisCache.hasKey("testUser"+id)){
            return (TestUser) redisCache.getCacheObject("testUser"+id);
        }
        redisCache.setCacheObject("testUser"+id,testUserMapper.selectById(id),10, TimeUnit.MINUTES);
        return testUserMapper.selectById(id);
    }

    @Override
    @Transactional
    public int insertTestUser(TestUser testUser) {
        testUserMapper.insertTestUser(testUser);
        return insertTestShops(testUser);
    }

    @Override
    @Transactional
    public int deleteTestUserByIds(Integer[] ids) {
        testUserMapper.deleteTestShopByShopIds(ids);
        return testUserMapper.deleteTestUserByIds(ids);
    }

    @Override
    @Transactional
    public int updateTestUser(TestUser testUser) {
        testUserMapper.deleteTestShopByShopId(testUser.getId());
        insertTestShops(testUser);
        return testUserMapper.updateTestUser(testUser);
    }

    @Override
    public int insertTestUsers(List<TestUser> testUsers) {
        return testUserMapper.insertTestUsers(testUsers);
    }

    public int insertTestShops(TestUser testUser){
        List<TestShop> shops = testUser.getTestShops();
        shops.forEach(testShop -> {testShop.setShopId(testUser.getId());});
        return testUserMapper.insertTestShops(shops);
    }
}
